package com.zj.study.pojo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.List;
import lombok.Getter;
import lombok.ToString;
import org.springframework.lang.NonNull;

/**
 * <p>统一分页对象<p/>
 *
 * @author 赵赳
 * @since 2021/9/17 14:28
 */
@Getter
@ToString
public class FinalPageVO2<T> implements Serializable {

  private final Boolean success;


  private final String code;


  private final String msg;

  private final RepData<T> data;


  public FinalPageVO2(Boolean success, String code, String msg,
      RepData<T> data) {
    this.success = success;
    this.code = code;
    this.msg = msg;
    this.data = data;
  }

  @NonNull
  public static <T> FinalPageVO2<T> ok(@NonNull IPage<T> page) {
    return ok(page, page.getRecords(), RespStatusEnum.OK.getMsg());
  }


  @NonNull
  public static <T> FinalPageVO2<T> ok(
      @NonNull IPage<?> page, @NonNull List<T> data, @NonNull String msg
  ) {
    return ok(new PageInfoVO()
            .setPage((int) page.getCurrent())
            .setTotalPage((int) page.getPages())
            .setSize((int) page.getSize())
            .setTotalSize((int) page.getTotal()),
        data, msg);
  }

  @NonNull
  public static <T> FinalPageVO2<T> ok(
      @NonNull PageInfoVO pages, @NonNull List<T> data, @NonNull String msg
  ) {
    return new FinalPageVO2<>(true, RespStatusEnum.OK.getCode(), msg,
        new RepData<>(pages, data));
  }

  @NonNull
  public static <T> FinalPageVO2<T> ok(
      @NonNull PageInfoVO pages, @NonNull List<T> data
  ) {
    return new FinalPageVO2<>(true, RespStatusEnum.OK.getCode(), RespStatusEnum.OK.getMsg(),
        new RepData<>(pages, data));
  }


}
